<template>
  <div class="container">
    <!--识别码对话框-->
    <el-dialog
      title="识别码"
      :visible.sync="dialogVisible"
      width="30%">
      <span>{{ this.listQuery.folder}}</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">关闭</el-button>
  </span>
    </el-dialog>
    <!--添加任务对话框-->
    <el-form :inline="true" :model="listQuery" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">添加上传任务</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible" :destroy-on-close="true">
      <el-form :model="listQuery">
        <el-form-item label="任务名称" :label-width="formLabelWidth">
          <el-input v-model="listQuery.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTask">确 定</el-button>
      </div>
    </el-dialog>
    <!--任务列表-->
    <el-table
      :data="fileTaskData"
      height="250"
      border
      v-loading="loading"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="任务名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="createUserName"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-share" @click="handleShareClick(scope.row)">分享</el-button>
          <el-button type="danger" size="small" @click="deleteTask(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[1, 10, 15]"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {addUploadTask, addUploadTaskCode, getUploadTaskList, removeTask} from "../../api/fileUpload";

  export default {
    name: "",
    data() {
      return {
        loading:false,
        fileTaskData: [],
        dialogFormVisible: false,
        formLabelWidth: '150px',
        total: 0,
        dialogVisible:false,
        listQuery: {
          page: 1,
          size: 10,
          name: '',
          folder:'',
          id:''
        }
      }
    },
    methods: {
      getTaskList() {
        this.loading=true
        getUploadTaskList(this.listQuery)
          .then(res => {
            if (res.code == 20000) {
              this.$message.success("查询成功");
              this.fileTaskData = res.data.pageList;
              this.total = res.data.total
            } else {
              this.$message.error(res.msg)
            }
            console.log(res)
            this.loading=false
          }).catch(err=>{
          this.$message.error(err.msg)
          this.loading=false
        })

      },
      //创建文件上传任务
      addTask() {
        this.loading=true
        this.dialogFormVisible = false
        addUploadTask(this.listQuery).then(res => {
          this.$message.success("添加成功")
          this.getTaskList()
          this.loading=false
        }).catch(err=>{
          this.loading=false
        })
      },
      //分页相关
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getTaskList()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getTaskList()
        console.log(`当前页: ${val}`);
      },
      handleShareClick(param) {
        this.listQuery.id=param.id
        addUploadTaskCode(this.listQuery).then(res => {
          this.listQuery.folder=res.data.folder
          this.dialogVisible=true
        })
      },
      //删除任务
      deleteTask(param) {
        this.loading=true
        removeTask(param).then(res=>{
          if (res.code == 20000) {
            this.$message.success("操作成功");
            this.getTaskList()
          } else {
            this.$message.error(res.msg)
          }
          this.loading=false
        }).catch(err=>{
          this.loading=false
        })
      }
    },
    created() {
      this.getTaskList()
    }
  }
</script>

<style scoped>
  .demo-form-inline {
    margin-top: 20px;
    margin-left: 10px;
  }
</style>
